//
//  ViewController.swift
//  FZSFImageBrowser-Example
//
//  Created by 王欣 on 2018/4/19.
//  Copyright © 2018年 王欣. All rights reserved.
//

import UIKit
import FZSFImageBrowser
import SDWebImage

class ViewController:UIViewController {
    var collectionView: UICollectionView!
    var imageBrowseViewController:FZBrowseViewController!
    var dataArr:[String]!
    var dataTArr:[Any]!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "大图缩放"

        dataArr = Array.init();
        dataArr.append("https://xiangyu.testx.fzyun.cn/testx/pic/1.jpeg")
        dataArr.append("https://xiangyu.testx.fzyun.cn/testx/pic/2.jpeg")
        dataArr.append("https://xiangyu.testx.fzyun.cn/testx/pic/3.jpeg")
        dataArr.append("https://xiangyu.testx.fzyun.cn/testx/pic/4.jpeg")
        
        dataTArr = ["第一个图片文案爱德克斯赶紧啊开了多久奥拉夫及类库的激发了开发经理肯定是","第二个图片文案爱德克斯赶紧啊开了多久奥拉夫及类库的激发了开发了；开发；阿里防守打法是开发 发经理肯定是","第三个图片文案爱德克斯赶紧电脑发老控件法兰克就发了快递费骄傲啊开了多久奥拉夫及类库的激发了开发经理肯定是","第四个图片文案爱德克"]
        
        view.backgroundColor = UIColor.white
        let flowlayout = Flowlayout()
        flowlayout.scrollDirection = .horizontal;
        flowlayout.itemSize = CGSize.init(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height);
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowlayout)
        self.view.addSubview(collectionView);
        collectionView.snp.makeConstraints { (make) in
//            make.center.equalToSuperview()
            make.left.equalTo(0)
            make.top.equalTo(20)
            make.width.equalToSuperview();
            make.height.equalTo(300).priority(.low);
            make.height.lessThanOrEqualToSuperview();
        }
        collectionView.backgroundColor = UIColor.clear;
        collectionView.dataSource = self;
        collectionView.delegate   = self;
        collectionView?.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        let baritem = UIBarButtonItem.init(title: "清除缓存", style: .done, target: self, action: #selector(clearData));
        self.navigationItem.rightBarButtonItem = baritem;
        
    }
    //清除缓存
    @objc func clearData() {
        SDImageCache.shared.clearDisk {
            print("清除完成")
        }
        SDImageCache.shared.clearMemory();
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewDidLayoutSubviews() {
        self.collectionView.reloadData();
    }


}

extension ViewController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CustomCollectionViewCell
        let path = dataArr[indexPath.row];
//        let image = UIImage.init(contentsOfFile: path);
        cell?.img.sd_setImage(with: URL.init(string: path), completed: nil)
//        cell?.img.image = image;
        return cell!
    }
    

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count;
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as?CustomCollectionViewCell else {
            return;
        }
        self.showImageBrowser(souceView: cell.img, index: indexPath.row);
    }
}

extension ViewController{
    
    func showImageBrowser(souceView:UIView,index:Int){
        imageBrowseViewController = FZBrowseViewController()
        if let imageBrowseViewController = imageBrowseViewController{
            imageBrowseViewController.sourceView   = souceView
            imageBrowseViewController.browseItems  = imageBrowseItems()
            imageBrowseViewController.currentIndex = index
            imageBrowseViewController.limiteMAXHeight = true
            imageBrowseViewController.whiteVlaue = 1
            imageBrowseViewController.holderImage = UIImage.init(named: "img");
            imageBrowseViewController.longPressClouse = {
                [weak self](_)in
                print("imageBrowseViewController longPressClouse")
            }
            let navigationController = UINavigationController(rootViewController: imageBrowseViewController)
            navigationController.modalPresentationStyle = .fullScreen
            self.present(navigationController, animated: false, completion: nil)
        }
    }
    
    func imageBrowseItems() ->[FZBrowseModel]{
        var imageBrowseItems = [FZBrowseModel]()
        for index in 0..<dataArr.count{
            let browseModel = FZBrowseModel()
            let imageUrl = dataArr[index];
            browseModel.imageUrl = imageUrl
            let str=dataTArr[index]
            browseModel.textS = [str]//
            browseModel.showText=dataTArr[index] as! String
            imageBrowseItems.append(browseModel)
        }
        return imageBrowseItems
    }
}



